package admin

import (
	"easyblog/logicals"
	"easyblog/models"
	"easyblog/utils/errmsg"
	"encoding/json"
	"github.com/beego/beego/v2/core/validation"
	"github.com/beego/beego/v2/server/web"
)

type ArticleController struct {
	web.Controller
}

// @router /admin/article [post]
func (this *ArticleController)AddArticle()  {
	var article models.Article

	body := this.Ctx.Input.RequestBody
	json.Unmarshal(body, &article)

	result := make(map[string]interface{})

	valid := validation.Validation{}
	b, _ := valid.Valid(&article)
	if !b {
		for _, err := range valid.Errors {
			result["status"] = errmsg.ERROR
			result["message"] = err.Key + " " + err.Message
			this.Data["json"] = result
			this.ServeJSON()
			this.StopRun()
		}
	}

	code := logicals.DefaultArticle.CreateArticle(&article)
	result["status"] = code
	result["message"] = errmsg.GetErrmsg(code)
	this.Data["json"] = result
	this.ServeJSON()
}

// @router /admin/article/:id [put]
func (this *ArticleController) EditArticle(){
	var article models.Article
	id, _ := this.GetInt(":id")

	body := this.Ctx.Input.RequestBody
	json.Unmarshal(body, &article)

	result := make(map[string]interface{})

	valid := validation.Validation{}
	b, _ := valid.Valid(&article)
	if !b {
		for _, err := range valid.Errors {
			result["status"] = errmsg.ERROR
			result["message"] = err.Key + " " + err.Message
			this.Data["json"] = result
			this.ServeJSON()
			this.StopRun()
		}
	}

	code := logicals.DefaultArticle.EditArticle(id, &article)
	result["status"] = code
	result["message"] = errmsg.GetErrmsg(code)
	this.Data["json"] = result
	this.ServeJSON()
}

// @router /admin/article/:id [get]
func (this *ArticleController) GetOneArticle() {
	id, _ := this.GetInt(":id")
	article, code := logicals.DefaultArticle.GetArticleById(id)

	result := map[string]interface{}{
		"status":  code,
		"data":    article,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}

// @router /admin/articles [get]
func (this *ArticleController) GetPageArticles(){
	pageSize, err := this.GetInt("pagesize")
	if pageSize < 1 || err != nil{
		pageSize = 5
	}
	pageNum, err := this.GetInt("pagenum")
	if pageNum < 1 || err != nil{
		pageNum = 1
	}

	data, code, total := logicals.DefaultArticle.GetPageArticles(pageSize, pageNum)

	result := map[string]interface{}{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}

// @router /admin/article/search [get]
func (this *ArticleController) SearchArticles(){
	keyWords := this.GetString("keywords")

	data,code,total :=  logicals.DefaultArticle.SearchArticles(keyWords)

	result := map[string]interface{}{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}


// @router /admin/article/:id [delete]
func (this *ArticleController) DeleteArticle() {
	id, _ := this.GetInt(":id")

	code := logicals.DefaultArticle.DeleteArticle(id)

	result := map[string]interface{}{
		"status":  code,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}

// @router /admin/article/top [put]
func (this *ArticleController) UpdateTop() {
	var id,top int
	this.Ctx.Input.Bind(&id,"id")
	this.Ctx.Input.Bind(&top,"top")

	code := logicals.DefaultArticle.UpdateTop(id,top)
	result := map[string]interface{}{
		"status":  code,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}

// @router /admin/article/reward [put]
func (this *ArticleController) UpdateReward() {
	var id,reward int
	this.Ctx.Input.Bind(&id,"id")
	this.Ctx.Input.Bind(&reward,"reward")

	code := logicals.DefaultArticle.UpdateReward(id,reward)
	result := map[string]interface{}{
		"status":  code,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}